function addCanBuy() {
	if (!validateGoodsName()) {
		return;
	}
	if (!validatePrice()) {
		return;
	}
	if (!validateRatio()) {
		return;
	}
	if (!validateDomesticPrice()) {
		return;
	}
	if (!validateShippingCost()) {
		return;
	}

	if (!validateImage()) {
		return;
	}

	if (trim($("#shippingCost").val()) == "") {
		$("#shippingCost").val(0);
	}

	var domesticPrice = $("#domesticPrice").val();
	if (domesticPrice != "" && domesticPrice != "国内无此产品") {
		$("#domesticPriceId").val($("#domesticPrice").val());
	}

	if (!submit) {
		submit = true;
		$("#form1").submit();
	}

}

function editCanBuy() {

	if (!validateGoodsName()) {
		return;
	}
	if (!validatePrice()) {
		return;
	}
	if (!validateRatio()) {
		return;
	}
	if (!validateDomesticPrice()) {
		return;
	}
	if (!validateShippingCost()) {
		return;
	}

	if (trim($("#shippingCost").val()) == "") {
		$("#shippingCost").val(0);
	}

	var domesticPrice = $("#domesticPrice").val();
	if (domesticPrice != "" && domesticPrice != "国内无此产品") {
		$("#domesticPriceId").val($("#domesticPrice").val());
	}

	if (!submit) {
		submit = true;
		$("#form1").submit();
	}
}

function validateImage() {
	var images = $("[name=image]");
	var empty = true;
	for ( var i = 0; i < images.length; i++) {
		if (images[i].value != "") {
			empty = false;
			break;
		}
	}
	if (empty) {
		$("#imagePrompt").show();
		return false;
	}
	return true;
}

function validateGoodsName() {
	var goodsName = trim($("#goodsName").val());
	if (goodsName == "") {
		$("#goodsNamePrompt").html("物品名称不能为空");
		$("#goodsNamePrompt").show();
		return false;
	} else {
		$("#goodsNamePrompt").hide();
		return true;
	}
}

function validatePrice() {
	var price = trim($("#price").val());
	if (price == "") {
		$("#pricePrompt").html("价格不能为空");
		$("#pricePrompt").show();
		return false;
	} else if (!priceRegex.test(price) || price <= 0) {
		$("#pricePrompt").html("请输入正确的价格");
		$("#pricePrompt").show();
		return false;
	} else {
		$("#pricePrompt").hide();
		return true;
	}
}

function validateRatio() {
	var ratio = trim($("#ratio").val());
	if (ratio == "") {
		$("#ratioPrompt").html("佣金比例不能为空");
		$("#ratioPrompt").show();
		return false;
	}
	if (!digitRegex.test(ratio) || ratio <= 0) {
		$("#ratioPrompt").html("只能输入正整数");
		$("#ratioPrompt").show();
		return false;
	}
	$("#ratioPrompt").hide();
	return true;
}

// 计算佣金
function calcServiceFee() {
	var price = $("#price").val();
	var ratio = $("#ratio").val();
	if (price != "" && priceRegex.test(price) && price > 0 && ratio != ""
			&& digitRegex.test(ratio) && ratio > 0) {
		var serviceFee = price * ratio / 100;
		$("#serviceFee").val(serviceFee.toFixed(2));
	}
}


function validateShippingCost(price) {
	alert(arguments.length);
	if (arguments.length == 0) {
		return validateShippingCost($("#shippingCost").val())
				&& validateShippingCost("#continuedPrice");

	} else {
		if (shippingCost != "" && (!priceRegex.test(price) || price < 0)) {
			$("#shippingCostPrompt").html("请输入正确的运费");
			$("#shippingCostPrompt").show();
			return false;
		}
		$("#shippingCostPrompt").hide();
	}

	return true;
}
